<template>
  <!-- 外包盒子 -->
  <el-container class="home_ctr">
    <!-- 头部组件 -->
    <el-header>
      <!-- 头部左右分，用div包裹住，里面放文本和头部图标 -->
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>电商管理系统</span>
      </div>
      <!-- 创建退出按钮 -->
      <el-button type="info" @click="loginOut">退出</el-button>
    </el-header>
    <el-container>
      <!-- 左边组件 -->
      <!-- 侧边栏随按钮点击进行变化 -->
      <el-aside :width="isCollape ? '64px' : '200px'">
        <div class="icon">
          <div class="el-icon-s-fold" @click="taggleCollape"></div>
        </div>
        <!-- 属性绑定，保持一个主菜单，开启折叠效果，关闭折叠动画,开启路由 -->
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409EFF"
          :unique-opened="true"
          :collapse="isCollape"
          :collapse-transition="false"
          :router="true"
        >
          <!-- 一级菜单 -->
          <!-- 动态数据绑定id v-for传值 -->
          <el-submenu
            :index="item.id.toString()"
            v-for="item in Menulist"
            :key="item.id"
          >
            <template slot="title">
              <!-- 图标 -->
              <i :class="IconObj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
            >
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右边组件 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  // date用来接收数据
  data () {
    return {
      Menulist: [],
      IconObj: {
        125: 'el-icon-s-custom',
        103: 'el-icon-s-cooperation',
        101: 'el-icon-s-goods',
        102: 'el-icon-tickets',
        145: 'el-icon-data-line'
      },
      isCollape: false
    }
  },
  created () {
    // 用getMeunList获取所以左侧菜单
    this.getMenuList()
  },
  methods: {
    loginOut () {
      // 清空token路径
      window.sessionStorage.clear('token')
      // 重定向跳转到/login
      this.$router.push('/login')
    },
    // 获取所有左侧菜单
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')

      if (res.meta.status !== 200) return this.$message.error(res.meta.res)
      this.Menulist = res.data
      console.log(res)
    },
    taggleCollape () {
      this.isCollape = !this.isCollape
    }
  }
}
</script>

<style lang="css" scoped>
.home_ctr {
  height: 100%;
}
/* 图标无左侧空白，按钮靠右，居中，字体白色，放大*/
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
}
/* 字体居中 */
.el-header div {
  display: flex;
  align-items: center;
}
/* 字体和图标向左15个像素 */
.el-header div span {
  margin-left: 15px;
}

.el-aside {
  background-color: #333744;
}

.el-main {
  background-color: #eaedf1;
}

.el-menu {
  border-right: none;
}

.icon {
  background-color: #4a5064;
  cursor: pointer;
}
/* 按钮样式设置 */
.el-icon-s-fold {
  font-size: 25px;
  display: flex;
  justify-content: center;
}
</style>
